package com.lxl.common.dto;

/**
 * @Description 返回实体类
 * @Date 2021/11/30 17:15
 * @Created by LXLDEMO
 */

public class ResultEntity {
    private Object data;
    private String code;
    private String message;

    public ResultEntity(Object data, String code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public ResultEntity() {
        this.code = Status.SUCCESS.getCode();
        this.message = Status.SUCCESS.getMessage();

    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static ResultEntity ofMessage(String code, String message) {
        return new ResultEntity(null, code, message);
    }

    /**
     * 请求成功
     *
     * @param data
     * @return
     */
    public static ResultEntity ofSuccess(Object data) {
        return new ResultEntity(data, Status.SUCCESS.getCode(), Status.SUCCESS.getMessage());
    }


    /**
     * 登录成功
     *
     * @param data
     * @return
     */
    public static ResultEntity loginSuccess(Object data) {
        return new ResultEntity(data, Status.LOGIN_SUCCESS.getCode(), Status.LOGIN_SUCCESS.getMessage());
    }


    /**
     * 登录失败
     *
     * @param data
     * @return
     */
    public static ResultEntity loginFail(Object data) {
        return new ResultEntity(data, Status.LOGIN_FAIL.getCode(), Status.LOGIN_FAIL.getMessage());
    }


    /**
     * token过期
     *
     * @return
     */
    public static ResultEntity tokenExpired() {
        return new ResultEntity(null, Status.TOKEN_EXPIRED.getCode(), Status.TOKEN_EXPIRED.getMessage());
    }


    /**
     * 无权限
     *
     * @param data
     * @return
     */
    public static ResultEntity noPermission(Object data) {
        return new ResultEntity(data, Status.FORBIDDEN.getCode(), Status.FORBIDDEN.getMessage());
    }

    /**
     * 自定义
     * @param data
     * @param code
     * @param message
     * @return
     */
    public static ResultEntity tmp(Object data, String code, String message){
        return new ResultEntity(data, code, message);
    }


    /**
     * 程序报错
     *
     * @param data
     * @return
     */
    public static ResultEntity error(Object data) {
        return new ResultEntity(data, Status.INTERNAL_SERVER_ERROR.getCode(), Status.INTERNAL_SERVER_ERROR.getMessage());
    }

    /**
     * 为excel某行格式报错创建
     * @param messag
     * @return
     */
    public static ResultEntity excelError(Object messag){
        return new ResultEntity(null, Status.INTERNAL_SERVER_ERROR.getCode(),messag.toString());
    }

    /**
     * 数据入库失败
     *
     * @param data
     * @return
     */
    public static ResultEntity insertError(Object data) {
        return new ResultEntity(data, Status.INSERT_ERROR.getCode(), Status.INSERT_ERROR.getMessage());
    }


    /**
     * 数据操作日志入库失败
     *
     * @param data
     * @return
     */
    public static ResultEntity dataLogError(Object data) {
        return new ResultEntity(data, Status.DATA_LOG_ERROR.getCode(), Status.DATA_LOG_ERROR.getMessage());
    }

    /**
     * 传入pageId为空
     *
     * @param data
     * @return
     */
    public static ResultEntity pageIdError(Object data) {
        return new ResultEntity(data, Status.PAGE_ID_ERROR.getCode(), Status.PAGE_ID_ERROR.getMessage());
    }

    /**
     * 缺少字段
     *
     * @param data
     * @return
     */
    public static ResultEntity missingField(Object data) {
        return new ResultEntity(data, Status.MISSING_FIELD.getCode(), Status.MISSING_FIELD.getMessage());
    }

    /**
     * 请求无效
     *
     * @param data
     * @return
     */
    public static ResultEntity badRequest(Object data) {
        return new ResultEntity(data, Status.BAD_REQUEST.getCode(), Status.BAD_REQUEST.getMessage());
    }

    /**
     * 查询结果为空
     * @param data
     * @return
     */
    public static ResultEntity emptyList(Object data) {
        return new ResultEntity(data, Status.EMPTY_LIST_ERROR.getCode(), Status.EMPTY_LIST_ERROR.getMessage());
    }

    /**
     * 缺少参数或参数输入错误
     * @param data
     * @return
     */
    public static ResultEntity paramError(Object data) {
        return new ResultEntity(data, Status.PARAM_ERROR_OR_MISSING_PARAM.getCode(), Status.PARAM_ERROR_OR_MISSING_PARAM.getMessage());
    }

    /**
     * 验证码失效
     * @param data
     * @return
     */
    public static ResultEntity codeInvalid(Object data){
        return new ResultEntity(data, Status.VERIFICATION_CODE_INVALID.getCode(), Status.VERIFICATION_CODE_INVALID.getMessage());
    }


    /**
     * 内部枚举类，列举常用请求返回状态码
     */
    public enum Status {
        /**
         * SUCCESS 请求成功
         * BAD_REQUEST 请求无效
         * FORBIDDEN 无权限访问
         * INTERNAL_SERVER_ERROR 未知服务器错误
         * LOGIN_ERROR 登录失败(用户名或密码不正确)
         * LOGIN_SUCCESS 登录成功
         */
        SUCCESS("200", "请求成功"),
        FAIL("", "请求失败"),
        BAD_REQUEST("400", "请求无效"),
        FORBIDDEN("403", "无权限访问"),
        INTERNAL_SERVER_ERROR("500", "未知服务器错误"),
        FIELD_NUM_ERROR("500", "excel字段超过数据库字段"),
        PAGE_ID_ERROR("500", "pageId不能为空"),
        INSERT_ERROR("500", "数据入库失败"),
        UPLOAD_ERROR("500", "文件上传异常"),
        DATA_LOG_ERROR("500", "数据操作日志入库失败"),
        FILE_LOG_ERROR("500", "文件操作日志入库/更新失败"),
        FILE_NAME_FORMAT_ERROR("500", "文件名格式错误"),
        EMPTY_LIST_ERROR("200","查询结果为空"),
        LOGIN_FAIL("415", "用户名或密码不正确"),
        TOKEN_EXPIRED("401", "token失效"),
        MISSING_FIELD("200", "缺少字段"),
        PARAM_ERROR_OR_MISSING_PARAM("200","缺少参数或参数输入错误"),
        LOGIN_SUCCESS("200", "登录成功"),
        VERIFICATION_CODE_INVALID("405", "验证码失效");

        private String code;
        private String message;

        Status(String code, String message) {
            this.code = code;
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "data=" + data +
                ", code='" + code + '\'' +
                ", message='" + message + '\'' +
                '}';
    }

}
